package com.primb.bench.project.search.model;

import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.approval.entity.ApprovalListEntity;
import com.primb.bench.project.search.contract.SearchListContract;
import com.primb.bench.project.search.entity.PriceListEntity;
import com.primb.bench.project.search.entity.ReapplyListEntity;
import com.primb.bench.project.search.entity.RequestPriceEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Administrator on 2017/07/17
 */

public class SearchListModelImpl implements SearchListContract.Model {

    @Override
    public Observable<List<ApprovalListEntity.DataBean>> getApprovalListInfo(RequestPriceEntity entity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("start", entity.getStart() + "");
        map.put("limit", entity.getLimit() + "");
        map.put("priceNegoType", "W01001");
        return ApiClient.initService(ApiService.class).getApprovalList(map).flatMap(new Func1<ApprovalListEntity, Observable<List<ApprovalListEntity.DataBean>>>() {
            @Override
            public Observable<List<ApprovalListEntity.DataBean>> call(ApprovalListEntity listEntity) {
                return Observable.just(listEntity.getData());
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<List<ReapplyListEntity.DataBean>> getReapplyListInfo(RequestPriceEntity entity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("start", entity.getStart() + "");
        map.put("limit", entity.getLimit() + "");
        map.put("priceNegoType", "W01001");
        return ApiClient.initService(ApiService.class).getSearchReapplyList(map).flatMap(new Func1<ReapplyListEntity, Observable<List<ReapplyListEntity.DataBean>>>() {
            @Override
            public Observable<List<ReapplyListEntity.DataBean>> call(ReapplyListEntity listEntity) {
                return Observable.just(listEntity.getData());
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<List<PriceListEntity.DataBean>> getPriceListInfo(RequestPriceEntity entity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("start", entity.getStart() + "");
        map.put("limit", entity.getLimit() + "");
        map.put("startDate", entity.getStartDate().replace("-", ""));

        if (entity.getEndDate() != null && !entity.getEndDate().equals(""))
            map.put("endDate", entity.getEndDate().replace("-", ""));

        if (entity.getCustId() != null && !entity.getCustId().equals(""))
            map.put("custId", entity.getCustId());

        if (entity.getCustName() != null && !entity.getCustName().equals(""))
            map.put("custName", entity.getCustName());

        if (entity.getPriceType() != null && !entity.getPriceType().equals(""))
            map.put("priceType", entity.getPriceType());

        if (entity.getLnTrem() != null && !entity.getLnTrem().equals(""))
            map.put("lnTrem", entity.getLnTrem());

        if (entity.getTremUtil() != null && !entity.getTremUtil().equals(""))
            map.put("tremUtil", entity.getTremUtil());

        if (entity.getOrgId() != null && !entity.getOrgId().equals(""))
            map.put("orgId", entity.getOrgId());

        if (entity.getProductId() != null && !entity.getProductId().equals(""))
            map.put("productId", entity.getProductId());

        if (entity.getLnPrdId() != null && !entity.getLnPrdId().equals(""))
            map.put("lnPrdId", entity.getLnPrdId());
        return ApiClient.initService(ApiService.class).getSearchPriceList(map).flatMap(new Func1<PriceListEntity, Observable<List<PriceListEntity.DataBean>>>() {
            @Override
            public Observable<List<PriceListEntity.DataBean>> call(PriceListEntity priceListEntity) {
                return Observable.just(priceListEntity.getData());
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }
}